﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using octopus_project.screen;
using octopus_project.scene.build.tetrad;
using octopus_project.scene;
using octopus_project.tileset;
using octopus_project.scene.war;
using octopus_project.scene.cannon;
using Microsoft.Xna.Framework;
using octopus_project.menu;

namespace octopus_project.typegame.traditionalWar {
    class TraditionalWar_TypeGame : TypeGame{
        private Build_Gameplay buildGamePlay;
        private Cannon_Gameplay cannonGamePlay;
        private War_Gameplay warGamePlay;
        private bool firstCannon = true;
        private Player playerLose = null;

        public TraditionalWar_TypeGame(ScreenManager screenManager)
            : base(screenManager) {

            
            initialize(screenManager);
            
        }

        private void initialize(ScreenManager screenManager) {
            //inicializamos los stats de acuerdo a este TypeGame
            foreach (Player aPlayer in Game1.Players.Values) {
                aPlayer.Stats = new TraditionalWar_Stats();
            }
            
            buildGamePlay = new Build_Gameplay(screenManager, this);
            gamePlaying = buildGamePlay;

            Game1.battlegroundMap = new TileMap_GameplayEntry(buildGamePlay, "../../../../octopus-projectContent/map/Map2.csv");
            Timer = new Timer_GameplayEntry(gamePlaying, new Vector2(748, 428), 20);

            buildGamePlay.createAll();
            LoadingScreen.Load(screenManager, true, null,gamePlaying);
        }

        public override void nextPhase() {

            //Pase de escena entre BuildGame a CannonGame
            if (buildGamePlay.GetType().IsInstanceOfType(gamePlaying)) {

                if (finishGame() == null) {
                    cannonGamePlay = new Cannon_Gameplay(base.screenManager, this);
                    gamePlaying = cannonGamePlay;
                    Timer = new Timer_GameplayEntry(gamePlaying, new Vector2(748, 428),10);
                    cannonGamePlay.createAll();
                    LoadingScreen.Load(screenManager, true, null, gamePlaying);
                    firstCannon = false;

                    //Game1.battlegroundMap.cleanAfterWar();
                }
                else {
                    

                    win();
                }

            }//Pase de escena entre CannonGame a WarGame
            else if (cannonGamePlay.GetType().IsInstanceOfType(gamePlaying)) {

                    
                    warGamePlay = new War_Gameplay(base.screenManager, this);
                    gamePlaying = warGamePlay;
                    Timer = new Timer_GameplayEntry(gamePlaying, new Vector2(748, 428),10);
                    warGamePlay.createAll();

                    //resetear los Stats, tiros etc.
                 /*   foreach (Player aPlayer in Game1.Players.Values) {
                        aPlayer.Stats.resetShots();
                    }*/

                    LoadingScreen.Load(screenManager, true, null, gamePlaying);



            }//Pase de escena entre WarGame a BuildGame
            else if (warGamePlay.GetType().IsInstanceOfType(gamePlaying)) {

                

                buildGamePlay = new Build_Gameplay(base.screenManager, this);
                gamePlaying = buildGamePlay;
                Timer = new Timer_GameplayEntry(gamePlaying, new Vector2(748, 428));
                buildGamePlay.createAll();
                LoadingScreen.Load(screenManager, true, null, gamePlaying);
                Game1.battlegroundMap.cleanAfterWar();
            }

            
        }

        //Rules to win
        private Player finishGame() {
            playerLose = null;
            if (!firstCannon) {

                foreach (Player aPlayer in Game1.Players.Values) {
                    
                    if (aPlayer.Stats.TerrainConquered == 0 || !aPlayer.Stats.HasCastle) {
                        playerLose = aPlayer;
                        break;
                    }
                    
                }
                
            }

            return playerLose;
        }


        protected void win() {

            string message = "Finish game. Lose " + playerLose.Name;
            MessageBoxScreen messageBox = new MessageBoxScreen(message);
            messageBox.Accepted += ConfirmExitMessageBoxAccepted;
            screenManager.AddScreen(messageBox);
        }


        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, PlayerIndexEventArgs e) {
            foreach (GameScreen screen in screenManager.GetScreens())
                screen.ExitScreen();

            screenManager.AddScreen(new BackgroundScreen());
            screenManager.AddScreen(new MainMenuScreen(screenManager));
        }


    }
}
